/******************************************************************************
 * \brief	通用的C语言标准库头文件包含，通用的宏定义
 * \note	File format: UTF-8，中文编码：UTF-8
 * \author	将狼才鲸
 * \date	2023-03-04
 ******************************************************************************/

#ifndef _UTIL_COMM_H_
#define _UTIL_COMM_H_

/*********************************** 头文件 ***********************************/
#include <stdint.h>		/* uint8_t uint16_t uint32_t uint64_t */
#include <stdbool.h>	/* bool true false */
#include <stdio.h>		/* printf scanf FILE fopen */
#include <string.h>		/* memcpy memset strlen */
#include <stdlib.h>		/* malloc free __max __min atoi atof atol labs rand */
#include <stddef.h>		/* NULL size_t wchar_t offsetof */

/*********************************** 宏定义 ***********************************/
#ifdef CONFIG_PRINT_OS_INFO
/* 操作系统判定 */
#	ifdef linux
#		warning "the os is linux"
#	elif WIN32
#		warning "the os is windows"
#	endif

/* 编译器判定 */
#	ifdef __GNUC__
#		warning "the compiler is linux gnuc gcc/g++"
#	elif _MSC_VER
#		warning "the compiler is windows vs"
#	elif defined(__C51__) || defined(__CX51__)
#		warning "the compiler is keil c51"
#	elif defined( __CC_ARM ) || defined(__CLANG_ARM)
#		warning "the compiler is mdk arm"
#	elif defined(Q_CC_MSVC) || defined(Q_CC_MINGW)
#		warning "the compiler is qt msvc or mingw"
#	endif

/* 大小端判定 */
#	if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#		warning "the CPU is big endian"
#	elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#		warning "the CPU is little endian"
#	endif

/* C语言标准判定 */
#	ifdef __STDC__
#		warning "the C language standard is ANSI C"
#	endif
#endif /* CONFIG_PRINT_OS_INFO */

//TODO: 如果在裸机板子上执行，则将下方替换成实际的中断屏蔽与打开的操作，防止在
//		中断中操作链表造成临界资源被破坏
//TODO:	如果在操作系统上运行，则将下方中断屏蔽替换成互斥锁
typedef int irq_flag;
#define irq_disable(flag)	/* 当前为空操作 */
#define irq_enable(flag)

/* 取消字节对齐 */
#if defined(__GNUC__)
#	ifndef __packed
#		define __packed __attribute__((__packed__))
#	endif /* __packed */
#else
#	define __packed
#endif /* __GNUC__ */

#define  PACKED __packed

/* ntohs等是网络字节顺序与本地字节顺序之间的转换函数，在#include <arpa/inet.h>中包含 */
#include <byteswap.h>	/* bswap_32... */
#if __BYTE_ORDER == __LITTLE_ENDIAN	/* Linux GNU C的大小端宏定义 */
#	define ntohl(x) bswap_32 (x)	/* 网络转主机长整型，network to host long int的缩写 */
#	define ntohs(x) bswap_16 (x)	/* network to host short int */
#	define htonl(x) bswap_32 (x)	/* host to network long int */
#	define htons(x) bswap_16 (x)	/* host to network short int */
#elif __BYTE_ORDER == __BIG_ENDIAN
#	define ntohl(x)   (x)
#	define ntohs(x)   (x)
#	define htonl(x)   (x)
#	define htons(x)   (x)
#endif

#endif /* _UTIL_COMM_H_ */

/*********************************** 文件尾 ***********************************/
